import { defineStore } from "pinia";

interface VideoState {
  pendingVideoTaskId: string | null;
}

export const useVideoStore = defineStore("video", {
  state: (): VideoState => ({
    // 初始化时尝试从localStorage获取pendingVideoTaskId
    pendingVideoTaskId: localStorage.getItem("pending-video-task") || null
  }),

  getters: {
    hasPendingTask: (state) => !!state.pendingVideoTaskId
  },

  actions: {
    // 设置待处理的视频任务ID
    setPendingTaskId(taskId: string) {
      this.pendingVideoTaskId = taskId;
      // 同时保存到localStorage以便页面刷新后恢复
      localStorage.setItem("pending-video-task", taskId);
    },

    // 清除待处理的视频任务
    clearPendingTask() {
      this.pendingVideoTaskId = null;
      localStorage.removeItem("pending-video-task");
    },

    // 从localStorage恢复任务ID（页面初始化时调用）
    restorePendingTask() {
      const taskId = localStorage.getItem("pending-video-task");
      if (taskId) {
        this.pendingVideoTaskId = taskId;
      }
    }
  }
});
